package com.sskj.mine.invite;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.ImgUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.CoinUtil;
import com.sskj.lib.util.CopyUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.bean.InviteInfo;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/05/03
 */
@Route(path = RConfig.MINE_INVITE)//邀请返佣
public class InviteActivity extends BaseActivity<InvitePresenter> {

    @BindView(R2.id.team_tv)
    TextView teamTv;
    @BindView(R2.id.team_layout)
    LinearLayout teamLayout;
    @BindView(R2.id.reward_tv)
    TextView rewardTv;
    @BindView(R2.id.reward_layout)
    LinearLayout rewardLayout;
    @BindView(R2.id.invite_code)
    TextView inviteCode;
    @BindView(R2.id.copy_img)
    ImageView copy;
    @BindView(R2.id.qr_code)
    ImageView qrCodeImg;
    @BindView(R2.id.ivBack)
    ImageView ivBack;


    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_invite;
    }

    @Override
    public InvitePresenter getPresenter() {
        return new InvitePresenter();
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        mPresenter.getShareInfo();
        ClickUtil.click(rewardLayout, () -> RewardActivity.start(this));  // 奖励
        ClickUtil.click(teamLayout, () -> TeamActivity.start(this));
        ClickUtil.click(copy, () -> {
            if (TextUtils.isEmpty(inviteCode.getText().toString().trim())) {
                return;
            }
            CopyUtil.copy(inviteCode.getText().toString().trim());
        });
        qrCodeImg.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                new RxPermissions(InviteActivity.this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        .subscribe(agree -> {
                            if (agree) {
                                if (ImgUtil.saveImageToGallery(InviteActivity.this, qrCodeImg)) {
                                    ToastUtil.showShort(getString(R.string.mine_inviteActivity1));
                                } else {
                                    ToastUtil.showShort(getString(R.string.mine_inviteActivity2));
                                }
                            } else {
                                ToastUtil.showShort(getString(R.string.mine_inviteActivity3));
                            }
                        });

                return true;
            }
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, InviteActivity.class);
        context.startActivity(intent);
    }

    public void updateUi(InviteInfo inviteInfo) {
        teamTv.setText(inviteInfo.getTeamCount());
        rewardTv.setText(CoinUtil.keepCoin(inviteInfo.getAll_money()));
        inviteCode.setText(inviteInfo.getAccount());
        ImageUtil.setImage(inviteInfo.getQrc(), qrCodeImg);
    }

}
